package com.addzero.kmp.screens.dept

import androidx.compose.runtime.Composable
import com.addzero.kmp.forms.SysDeptForm
import com.addzero.kmp.forms.SysDeptFormDsl
import com.addzero.kmp.forms.rememberSysDeptFormState
import com.addzero.kmp.viewmodel.SysDeptViewModel
import org.koin.compose.viewmodel.koinViewModel

@Composable
fun RenderDeptForm(vm: SysDeptViewModel) {

    val vm = koinViewModel<SysDeptViewModel>()

    val rememberSysDeptFormState = rememberSysDeptFormState()
    SysDeptForm(
        rememberSysDeptFormState,
        visible = vm.showForm,
        title = "部门表单",
        onClose = { vm.showForm = false },
        onSubmit = { vm.onSave(rememberSysDeptFormState.value) }
    ) {
        renderParent(vm)
        children(true)
        sysUsers(true)
    }
}

private fun SysDeptFormDsl.renderParent(vm: SysDeptViewModel) {

    parent {


        LeftCard(
            vm = vm,
            onNodeClick = {
                state.value = state.value.copy(parent = it)
            }
        )



//        //默认选中上级
//        state.value = state.value.copy(parent = vm.currentDeptVO)
//        var formKeyWord by remember { mutableStateOf("") }
//
//        LaunchedEffect(formKeyWord) {
//            vm.viewModelScope.launch {
//
//            }
//
//
//        }
//
//
//        Box(modifier = Modifier.Companion.fillMaxSize()) {
//            Column {
//                AddSearchBar(
//                    keyword = formKeyWord,
//                    onKeyWordChanged = { formKeyWord = it },
//                    onSearch = { vm.loadDeptTree() }
//                )
//
//                AddTree(
//                    items = vm.deptVos,
//                    getId = { it.id!! },
//                    getLabel = { it.name },
//                    getChildren = { it.children },
//                    onCurrentNodeClick = {
//                        state.value = state.value.copy(parent = it)
//                    },
//                )
//
//            }
//
//        }


    }
}